package com.bigdata.mapreduce.avg;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class WordCount {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//        1.从本地加载配置文件
        Configuration conf = new Configuration(true);

//        在windows下并且是local本地模式执行mr任务，需要添加配置
//        windows异构平台运行
        conf.set("mapreduce.app-submission.cross-platform", "true");
//        决定了集群运行
        conf.set("mapreduce.framework.name", "yarn");

//        2.使用配置对象创建job
        Job job = Job.getInstance(conf);
        job.setJar("D:\\bigdata_project\\bigdata_2307\\bigdata_hadoop\\target\\bigdata_hadoop-1.0-SNAPSHOT.jar");

//        3.设置任务的主启动类，
        job.setJarByClass(WordCount.class);
        job.setJobName("WordCount");

//        4.设置HDFS输入、输出路径
        String input = "/data/avg/input";
        String output = "/data/avg/output";
        Path inputPath = new Path(input);
        Path outputPath = new Path(output);
        TextInputFormat.addInputPath(job, inputPath);

//        判断目录是否存在，存在就删除
        if (outputPath.getFileSystem(conf).exists(outputPath)) {
            outputPath.getFileSystem(conf).delete(outputPath, true);
        }
        TextOutputFormat.setOutputPath(job, outputPath);

//        5.设置Mapper类、Mapper的输出key、value类型
        job.setMapperClass(WordMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

//        6.设置reducer类。reducer输出类型
        job.setReducerClass(WordReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // combiner优化
        job.setCombinerClass(AvgCombiner.class);


//        7.提交任务，true 表示监控任务
        job.waitForCompletion(true);
    }
}
